//
//  KKUploadUtils.m
//  DoMi
//
//  Created by 康凯 on 2016/12/2.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKUploadUtils.h"


@implementation KKUploadUtils
// 异步上传
- (void)uploadObjectAsync:(NSData *)uploadImgData{
    id<OSSCredentialProvider> credential=[[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:ACCESSKEY secretKey:SECRETKEY];
    OSSClientConfiguration * conf = [OSSClientConfiguration new];
    conf.maxRetryCount = 2;
    conf.timeoutIntervalForRequest = 30;
    conf.timeoutIntervalForResource = 24 * 60 * 60;
    self.client=[[OSSClient alloc] initWithEndpoint:ENDPOINT credentialProvider:credential clientConfiguration:conf];
    
    
    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
    
    NSDate *date = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd_HH:mm:ss"];
    NSString *ds = [fmt stringFromDate:date];
    
    put.bucketName = BUCKET_NAME;
    put.objectKey = [NSString stringWithFormat:@"%@.jpg",ds];
    put.uploadingData=uploadImgData;
    
    //    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    hud.mode = MBProgressHUDModeDeterminate;
    //    hud.label.text = @"正在上传图片";
    put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
        dispatch_sync(dispatch_get_main_queue(), ^{//其实这个也是在子线程中执行的，只是把它放到了主线程的队列中
            Boolean isMain = [NSThread isMainThread];
            if (isMain) {
                [SVProgressHUD showProgress:totalByteSent/totalBytesExpectedToSend status:@"正在上传图片"];
                
                //                float progress = 0.0f;
                //                progress=totalByteSent/totalBytesExpectedToSend;
                //                [MBProgressHUD HUDForView:self.view].progress = progress;
                //                if(progress==1.0f){
                //                    [hud hideAnimated:YES];
                //                }
                
            }
        });
//        NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
    };
    
    OSSTask * putTask = [self.client putObject:put];
    [putTask continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            NSString *url=[NSString stringWithFormat:@"https://%@.%@/%@.jpg?x-oss-process=image/resize,w_600",BUCKET_NAME,@"oss-cn-beijing.aliyuncs.com",ds];
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.uploadBlock(url);
                [SVProgressHUD dismiss];
            });
            
            NSLog(@"upload object success!");
        } else {
            dispatch_sync(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                [KKAppUtils toast:@"上传失败"];
            });
            NSLog(@"upload object failed, error: %@" , task.error);
        }
        return nil;
    }];
}

-(void)uploadRecorderAudio:(NSData *)recorder{
    id<OSSCredentialProvider> credential=[[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:ACCESSKEY secretKey:SECRETKEY];
    OSSClientConfiguration * conf = [OSSClientConfiguration new];
    conf.maxRetryCount = 2;
    conf.timeoutIntervalForRequest = 30;
    conf.timeoutIntervalForResource = 24 * 60 * 60;
    self.client=[[OSSClient alloc] initWithEndpoint:ENDPOINT credentialProvider:credential clientConfiguration:conf];
    
    
    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
    
    NSDate *date = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd_HH:mm:ss"];
    NSString *ds = [fmt stringFromDate:date];
    
    put.bucketName = BUCKET_NAME;
    put.objectKey = [NSString stringWithFormat:@"%@.aac",ds];
    put.uploadingData=recorder;
    OSSTask * putTask = [self.client putObject:put];
    [putTask continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            NSString *url=[NSString stringWithFormat:@"http://%@.%@/%@.aac",BUCKET_NAME,@"oss-cn-beijing.aliyuncs.com",ds];
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.uploadBlock(url);
            });
            NSLog(@"upload object success!");
        } else {
            NSLog(@"upload object failed, error: %@" , task.error);
        }
        return nil;
    }];
}
@end
